package collecion.nexd.com.newnexdcollection.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import collecion.nexd.com.newnexdcollection.R;
import collecion.nexd.com.newnexdcollection.modle.FileAttrObject;

/**
 * Created by xu on 2015/8/17.
 */
public class SlidingMenuAdapter extends BaseAdapter {

    private ArrayList<FileAttrObject> attrs;
    private LayoutInflater inflater;
    private Activity activity;

    public SlidingMenuAdapter(Activity activity) {
        this.activity = activity;
        this.attrs = new ArrayList<>();
        inflater = activity.getLayoutInflater();
    }


    public void clear() {
        this.attrs.clear();
    }

    public void addData(FileAttrObject attr) {
        this.attrs.add(attr);
    }

    @Override
    public int getCount() {
        return attrs.size();
    }

    @Override
    public FileAttrObject getItem(int i) {
        return attrs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = inflater.from(activity).inflate(R.layout.sliding_listview_item, null);
            viewHolder.data_name = (TextView) view.findViewById(R.id.tv_coll_data_name);
            viewHolder.data_size = (TextView) view.findViewById(R.id.tv_coll_data_size);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (attrs.size() > 0) {
            FileAttrObject attr = this.getItem(i);
            String data_name = attr.getFileName();
            viewHolder.data_name.setText(attr.getFileName());
            viewHolder.data_size.setText(attr.getFileSize() + "KB");
        }
        return view;
    }

    class ViewHolder {
        TextView data_name;
        TextView data_size;
    }
}
